package com.anyext.storage.asm;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.anyext.storage.jdbc.RowMapper;
import com.anyext.storage.model.Address;
import com.anyext.storage.model.DeptInfo;
import com.anyext.storage.model.UserInfo;

public class UserMapper implements RowMapper<UserInfo> {

	private Map<String, UserInfo>	businessMap	= new HashMap<String, UserInfo>();

	@Override
	public UserInfo mapRow(ResultSet rs, int rowNum) throws SQLException {

		StringBuffer userInfoKey = new StringBuffer();
		String platUsername = rs.getString("platUsername");
		userInfoKey.append(platUsername);
		Date registerTime = rs.getTimestamp("registerTime");
		userInfoKey.append(registerTime);
		String deptCode = rs.getString("deptCode");
		userInfoKey.append(deptCode);
		int deptNumber = rs.getInt("deptNumber");
		// ……

		UserInfo userInfo = businessMap.get(userInfoKey.toString());
		if (userInfo == null) {
			userInfo = new UserInfo();
			userInfo.setPlatUsername(platUsername);
			userInfo.setRegisterTime(registerTime);
			// ……

			{
				DeptInfo deptInfo = new DeptInfo();
				deptInfo.setDeptCode(deptCode);
				deptInfo.setDeptNumber(deptNumber);
				// ……

				userInfo.setDeptInfo(deptInfo);
			}
			{
				DeptInfo deptInfo = new DeptInfo();
				deptInfo.setDeptCode(deptCode);
				deptInfo.setDeptNumber(deptNumber);
				// ……

				userInfo.setDeptInfo(deptInfo);
			}
			businessMap.put(userInfoKey.toString(), userInfo);
		}
		{
			StringBuffer addressesKey = new StringBuffer(userInfoKey);
			String address1 = rs.getString("address1");
			addressesKey.append(address1);
			String address2 = rs.getString("address2");
			if (!businessMap.containsKey(addressesKey.toString())) {
				List<Address> addresses = userInfo.getAddress();
				if (addresses == null) {
					addresses = new ArrayList<Address>();
					userInfo.setAddress(addresses);
				}
				{
					Address address = new Address();
					address.setAddress1(address1);
					address.setAddress2(address2);
					addresses.add(address);
				}
				businessMap.put(addressesKey.toString(), null);
			}
		}
		{
			StringBuffer addressesKey = new StringBuffer(userInfoKey);
			String address1 = rs.getString("address1");
			addressesKey.append(address1);
			String address2 = rs.getString("address2");
			if (!businessMap.containsKey(addressesKey.toString())) {
				List<Address> addresses = userInfo.getAddress();
				if (addresses == null) {
					addresses = new ArrayList<Address>();
					userInfo.setAddress(addresses);
				}
				{
					Address address = new Address();
					address.setAddress1(address1);
					address.setAddress2(address2);
					addresses.add(address);
				}
				businessMap.put(addressesKey.toString(), null);
			}
		}
		// ……

		return userInfo;
	}

}
